//
//  StatusCell.m
//  WeiboSpace
//
//  Created by  linan4 on 12-3-11.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "StatusCell.h"
#import "ImageDLoader.h"
#import "ImageLoaderUtility.h"
//控件TAG定义
#define SCREEN_TAG  01
#define HEAD_TAG 02
#define TEXT_TAG 03
#define STATUS_THUMB_TAG 04
#define RETWEET_VIEW	05
#define RETWEET_STATUS	06
#define	RETWEET_STATUS_THUMB_TAG	07
//元素rect定义
#define ROW_HEIGHT 80
#define BORDER_SIZE 7 
#define HEAD_SIZE	30
#define NICK_WIDTH	200
#define NICK_HEIGHT	15
#define GAP1_SIZE	7
#define THUMB_SIZE	70
#define CONTENT_XOFFSET BORDER_SIZE+HEAD_SIZE+GAP1_SIZE
#define CONTENT_YOFFSET BORDER_SIZE+NICK_HEIGHT+GAP1_SIZE
#define CONTENT_WIDTH	320-2*BORDER_SIZE-HEAD_SIZE-GAP1_SIZE

@implementation StatusCell

- (id)initCell:(EnumCellOption) option delegate:(NSObject<StatusCellDelegate>*)newDelegate {
    
    self = [super init];
    if (self) {
        // Initialization code.
		cellOption = option;
		_delegate = newDelegate;
		[self createCell];
    }
    return self;
}

-(void)createCell{
	
	CGRect rect;
	
	//头像
	rect = CGRectMake(BORDER_SIZE, BORDER_SIZE, HEAD_SIZE, HEAD_SIZE);
	headImage = [[UIImageView alloc] initWithFrame:rect];
	//[headImage setTag:HEAD_TAG];
	[self.contentView addSubview:headImage];
	//[headImage release];
	
	//昵称
	rect = CGRectMake(CONTENT_XOFFSET , BORDER_SIZE, NICK_WIDTH, NICK_HEIGHT);
	screenNamelable = [[UILabel alloc] initWithFrame:rect];
	screenNamelable.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]];
	screenNamelable.highlightedTextColor = [UIColor whiteColor];
	screenNamelable.opaque = NO;
	//[screenNamelable setTag:SCREEN_TAG];
	[self.contentView addSubview:screenNamelable];
	//[screenNamelable release];
	
	//status文字内容
	rect = CGRectMake(CONTENT_XOFFSET, CONTENT_YOFFSET, CONTENT_WIDTH, 0);
    statusLabel = [[UILabel alloc] initWithFrame:rect];
	statusLabel.font = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
	//contentlable.textAlignment = UILineBreakModeClip;
	statusLabel.highlightedTextColor = [UIColor whiteColor];
	statusLabel.opaque = NO;
	[statusLabel setTag:TEXT_TAG];
	statusLabel.numberOfLines = 0;
	[self.contentView addSubview:statusLabel];
	//[statusLabel release];
	
	if (cellOption == ShowAllStuff) {
		
		//stauts的缩略图
		rect = CGRectMake(0, 0, 0, 0);
		statusThumbImage= [[UIImageView alloc] initWithFrame:rect];
		[statusThumbImage setTag:STATUS_THUMB_TAG];
		[self.contentView addSubview:statusThumbImage];
		//[statusThumbImage release];
		
		//透明Button
		//缩略图的透明button
		
		clickButton = [[TransparentButton alloc] initWithFrame:rect];
		[clickButton addTarget:self action:@selector(thumbTranButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
		clickButton.opaque = NO;
		clickButton.backgroundColor = [UIColor clearColor];
		[self.contentView addSubview:clickButton];
		//[clickButton release];
	}

	 
	
	
	//==转发内容View==/
	
	 //retweetView
	 rect = CGRectMake(CONTENT_XOFFSET, CONTENT_YOFFSET, 0, 0);
	 retweetView= [[UIView alloc] initWithFrame:rect];
	 retweetView.backgroundColor = [UIColor lightGrayColor];
	 
	 
	 rect = CGRectMake(GAP1_SIZE, GAP1_SIZE, CONTENT_WIDTH-GAP1_SIZE, 0);
	 retweet_statusLabel = [[UILabel alloc] initWithFrame:rect];
	 retweet_statusLabel.font = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
	 //contentlable.textAlignment = UILineBreakModeClip;
	 retweet_statusLabel.highlightedTextColor = [UIColor whiteColor];
	 retweet_statusLabel.backgroundColor=[UIColor clearColor];
	 retweet_statusLabel.opaque = NO;
	 retweet_statusLabel.numberOfLines = 0;
	 [retweetView addSubview:retweet_statusLabel];
	 //[retweet_statusLabel release];
	 
	if (cellOption == ShowAllStuff) {
		
		rect = CGRectMake(GAP1_SIZE, GAP1_SIZE, 0, 0);
		retweet_statusThumb= [[UIImageView alloc] initWithFrame:rect];
		[retweetView addSubview:retweet_statusThumb];
		//[retweet_statusThumb release];
		
		//透明Button
		//缩略图的透明button
		
		thumclickButton = [[TransparentButton alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
		[thumclickButton addTarget:self action:@selector(thumbTranButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
		thumclickButton.opaque = NO;
		thumclickButton.backgroundColor = [UIColor clearColor];
		[retweetView addSubview:thumclickButton];
		//[thumclickButton release];
	}	 
	 //
	 [self.contentView addSubview:retweetView];
	 //[retweetView release];
	 
	
}

-(void)updateCell:(NSDictionary *)statusData{
	
	if (statusData==nil) {
		return;
	}
	CGRect cellRect = [self frame];
	CGRect rect;
	//NSDictionary *statusData = [timeLine objectAtIndex:rowIndex];
	NSDictionary *userData = [statusData objectForKey:@"user"];
	NSDictionary *retweetData = [statusData objectForKey:@"retweeted_status"];
	
	
	//NSDictionary * status = [timeLine objectAtIndex:rowIndex];
	CGFloat statusHeight = 0;
	statusHeight+=BORDER_SIZE;
	//设置screenName
	//UILabel *screenNameLable = (UILabel*)[cell viewWithTag:SCREEN_TAG];	
	screenNamelable.text = [userData objectForKey:@"screen_name"];
	statusHeight+=NICK_HEIGHT;
	
	//设置头像
	//UIImageView *headImage = (UIImageView*)[cell viewWithTag:HEAD_TAG];
	NSString *headUrl = [userData objectForKey:@"profile_image_url"];
	
	//下载，走着->
//	ImageDLoader *imageDown = [[ImageDLoader alloc]init];
//	[imageDown getImageFromURL:headUrl UiImage:headImage.i delegate:nil];
//	[imageDown release];
    //test
    //[[XImageLoader shareXImageLoader] setImageWithURL:headUrl toView:headImage];
    [headImage setImageViewWithUrl:headUrl placeholderImage:nil];
    //test
	
	statusHeight+=GAP1_SIZE;
	//设置status
	//UILabel *statusLable = (UILabel*)[cell viewWithTag:TEXT_TAG];
	statusLabel.text = [statusData objectForKey:@"text"];
	[statusLabel sizeToFit];
	CGFloat statusHeight0 = statusLabel.frame.size.height;
	statusHeight+=statusHeight0;
	statusHeight+=GAP1_SIZE;
	
	if (cellOption == ShowAllStuff) {
		
		//thumb缩略图
		NSString *thumbUrl = [statusData objectForKey:@"thumbnail_pic"];
		if (thumbUrl!=nil&&[thumbUrl length]>0) {
			//UIImageView *thumbImage = (UIImageView*)[cell viewWithTag:STATUS_THUMB_TAG];
			rect = CGRectMake(CONTENT_XOFFSET+GAP1_SIZE, statusHeight, THUMB_SIZE, THUMB_SIZE);
			[statusThumbImage setFrame:rect];
			
			//**覆盖透明的clickButton
			[clickButton setFrame:rect];
			clickButton.markValue = [statusData objectForKey:@"bmiddle_pic"];
			
			statusHeight+=THUMB_SIZE;
			statusHeight+=GAP1_SIZE*2;
			
			//下载，走着->
            [statusThumbImage setImageViewWithUrl:thumbUrl placeholderImage:nil];
		}
	}

	
	//==以下是转发内容==//
	CGFloat retweetHeight=0;
	
	 if(retweetData != nil){
	 
		 retweetHeight += GAP1_SIZE;
		 //status
		 
		 //重设retweetStatus区域，高度自动化
		 //rect = CGRectMake(GAP1_SIZE, GAP1_SIZE, CONTENT_WIDTH, 0);
		 //retweet_statusLabel.text = [retweetData objectForKey:@"text"];
		 retweet_statusLabel.text = [NSString stringWithFormat:@"%@: %@",[[retweetData objectForKey:@"user"]objectForKey:@"screen_name"]
									 ,[retweetData objectForKey:@"text"]];
		 
		 [retweet_statusLabel sizeToFit];
		 CGFloat statusHeight0 = retweet_statusLabel.frame.size.height;
		 retweetHeight += statusHeight0;
		 retweetHeight += GAP1_SIZE;
		 
		 if (cellOption == ShowAllStuff) {
			 //thumb
			 NSString *thumbUrl0 = [retweetData objectForKey:@"thumbnail_pic"];
			 if (thumbUrl0!=nil&&[thumbUrl0 length]>0) {
				 
				 //重设thumb区域
				 rect = CGRectMake(GAP1_SIZE, retweetHeight+GAP1_SIZE, THUMB_SIZE, THUMB_SIZE);
				 [retweet_statusThumb setFrame:rect];
				 
				 //**覆盖透明的clickButton
				 [thumclickButton setFrame:rect];
				 thumclickButton.markValue = [retweetData objectForKey:@"bmiddle_pic"];
				 
				 retweetHeight += GAP1_SIZE;
				 retweetHeight += THUMB_SIZE;
				 //下载，走着->
                 [retweet_statusThumb setImageViewWithUrl:thumbUrl0 placeholderImage:nil];
			 }
		 }

		 
		 rect = CGRectMake(CONTENT_XOFFSET+GAP1_SIZE, statusHeight+GAP1_SIZE, CONTENT_WIDTH-GAP1_SIZE, retweetHeight);
		 [retweetView setFrame:rect];
	 
	 }
	 
	
	//重新设置cell高度
	cellRect.size.height = statusHeight + retweetHeight+/*test*/GAP1_SIZE*2;
	[self setFrame:cellRect];
	
}
-(void)thumbTranButtonClicked:(id)sender{
	TransparentButton *button = (TransparentButton*)sender;
	if (_delegate != nil) {
		[_delegate CellButtonClicked:button.markValue];
	}
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state.
}

#pragma mark ImageDLoaderDelegate
- (void)receivedImage:(NSData*)imageData sender:(ImageDLoader*)sender{
	
}

- (void)dealloc {

	[headImage release];
	[screenNamelable release];
	[statusLabel release];
	[statusThumbImage release];
	[clickButton release];
	
	[retweetView release];
	[retweet_statusLabel release];
	[retweet_statusThumb release];
	[thumclickButton release];
	_delegate = nil;
    [super dealloc];
}


@end
